//
//  TextEditorViewController.m
//  XPhoto
//
//  Created by sentinel on 2020/4/7.
//  Copyright © 2020 baboy. All rights reserved.
//

#import "TextEditorViewController.h"

@interface TextEditorViewController ()<UITextViewDelegate>

@end

@implementation TextEditorViewController
- (id)init{
    if (self = [super initWithNibName:NSStringFromClass([self class]) bundle:nil]){
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.textView.text = self.defaultText;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image.png"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(cancel:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image.png"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(done:)];
    self.textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
    [self resizeTextView];
}
- (void)resizeTextView{
    NSString *text = self.textView.text?self.textView.text : @"Test";
    CGSize textSize = [text boundingRectWithSize:CGSizeMake(self.textView.bounds.size.width - self.textView.textContainerInset.left- self.textView.textContainerInset.right,CGFLOAT_MAX)
                                         options:NSStringDrawingUsesLineFragmentOrigin
                                      attributes:@{NSFontAttributeName:self.textView.font}
                                         context:nil].size;
    
    CGRect textViewFrame = self.textView.frame;
    textViewFrame.size.height = self.textView.textContainerInset.top + self.textView.textContainerInset.bottom + textSize.height;
    self.textView.frame = textViewFrame;
}
#pragma navbar
- (void)cancel:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(textEditorViewControllerDidCanceledEdit:)]) {
        [self.delegate textEditorViewControllerDidCanceledEdit:self];
    }
}
- (void)done:(id)sender{    
    if (self.delegate && [self.delegate respondsToSelector:@selector(textEditorViewControllerDidFinishedEdit:)]) {
        [self.delegate textEditorViewControllerDidFinishedEdit:self];
    }
}
#pragma UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView{
    DLOG(@"text content size:%@", NSStringFromCGSize(textView.contentSize));
    [self resizeTextView];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
